package com.tsugun.mediaplayer.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.AlbumModel;
import com.tsugun.mediaplayer.model.entity.ArtistModel;
import com.tsugun.mediaplayer.model.entity.MusicModel;
import com.tsugun.mediaplayer.presenter.provider.ListProvider;
import com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper;
import com.tsugun.mediaplayer.view.activity.IndexActivity;
import com.tsugun.mediaplayer.view.adapter.ViewPagerAdapter;
import com.tsugun.mediaplayer.view.fragment.subfragment.AlbumListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.ArtistListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.FolderListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.SongListFragment;
import com.tsugun.mediaplayer.view.fragment.subfragment.SubBaseFragment;
import com.tsugun.mediaplayer.view.widget.SlidingRelativeLayout;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 本地音乐界面
 * Created by shize on 2017/4/20.
 */

public class LocalFragment extends BaseFragment {

    @BindView(R.id.id_sliding_body)
    SlidingRelativeLayout mSlidingLayout;
    @BindView(R.id.id_bg_night)
    RelativeLayout mBgLayout;
    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_tab)
    TabLayout mTabLayout;
    @BindView(R.id.id_pager)
    ViewPager mViewPager;

    private View mFragment;
    private Context mContext;
    private Unbinder unbinder;
    private IndexActivity indexActivity;
    private ListProvider mProvider;

    private List<String> mTitles;
    private List<SubBaseFragment> mFragments;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mFragment != null) {
            return mFragment;
        }
        mFragment = inflater.inflate(R.layout.fragment_local, container, false);
        mContext = container.getContext();
        unbinder = ButterKnife.bind(this, mFragment);

        initData();
        bindData();
        return mFragment;
    }

    @Override
    protected void setSettingTheme() {
        int themeColor = indexActivity.getThemeColor();
        mToolbar.setBackgroundColor(themeColor);
        mTabLayout.setBackgroundColor(themeColor);
        mBgLayout.setBackgroundColor(SettingPreferenceHelper.INSTANCE.getBACKGROUND_COLOR());
    }

    private void initData() {
        indexActivity = (IndexActivity) mContext;
        mProvider = indexActivity.getListProvider();

        mTitles = new ArrayList<>();
        mTitles.add(getString(R.string.str_tab_song));
        mTitles.add(getString(R.string.str_tab_artist));
        mTitles.add(getString(R.string.str_tab_album));
        mTitles.add(getString(R.string.str_tab_folder));

        mFragments = new ArrayList<>();
        mFragments.add(new SongListFragment());
        mFragments.add(new ArtistListFragment());
        mFragments.add(new AlbumListFragment());
        mFragments.add(new FolderListFragment());
    }

    private void bindData() {
        bindSliding();
        bindListData();
        bindToolbar();
        bindPage();
    }

    private void bindPage() {
        mViewPager.setAdapter(new ViewPagerAdapter(indexActivity.getSupportFragmentManager(),
                mFragments, mTitles));
        for (String title : mTitles) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
        }
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(1);
    }

    private void bindToolbar() {
        mToolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_arrow_back, null));
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                indexActivity.getMusicFragmentManager().backFragment();
            }
        });
        mToolbar.setTitle(getString(R.string.str_phone_music));
    }

    /**
     * 绑定滑动
     */
    private void bindSliding() {
        mSlidingLayout.setOnSlidingListener(new SlidingRelativeLayout.OnSlidingListener() {
            @Override
            public void onDown() {

            }

            @Override
            public void onAnimationEnd(boolean isClose) {
                if (isClose) {
                    indexActivity.getMusicFragmentManager().backFragment();
                }
            }
        });
    }

    /**
     * 绑定listFragment的数据
     */
    private void bindListData() {
        List<MusicModel> songs = new ArrayList<>();
        List<ArtistModel> artists = new ArrayList<>();
        List<AlbumModel> albums = new ArrayList<>();
        List<String> folders = new ArrayList<>();

        songs.addAll((Collection<MusicModel>) mProvider.searchAllMusic());
        artists.addAll((Collection<ArtistModel>) mProvider.getArtist());
        albums.addAll((Collection<AlbumModel>) mProvider.getAlbum());
        folders.addAll((Collection<String>) mProvider.getFolder());

        ((SongListFragment) mFragments.get(0)).setResult(songs);
        ((ArtistListFragment) mFragments.get(1)).setResult(artists);
        ((AlbumListFragment) mFragments.get(2)).setResult(albums);
        ((FolderListFragment) mFragments.get(3)).setResult(folders);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        clearFragments(indexActivity, mFragments);
        super.onDestroy();
        unbinder.unbind();
    }
}
